package com.sxc.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/ser01")
public class Servlet01 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 完整的请求url
        String url = req.getRequestURL().toString();
        System.out.println("获取客户端请求的完整url: " + url);
        // 2. 部分url(站点名开始，到？前面结束)
        String uri = req.getRequestURI();
        System.out.println("客户端请求的部分url: " + uri);
        // 3. 获取请求行中的参数部分
        String queryString = req.getQueryString();
        System.out.println("获取请求行中的参数部分: " + queryString);
        // 4. 获取客户端请求方式
        String method = req.getMethod();
        System.out.println("获取客户端的请求方式: " + method);
        // 5. 获取http版本号
        String protocol = req.getProtocol();
        System.out.println("http版本号为: " + protocol);
        // 6. 获取webapp名字
        String contextPath = req.getContextPath();
        System.out.println("webapp名字为: " + contextPath);
        // 7. 获取请求参数
            // (1)、获取指定名称参数
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        System.out.println("username: " + username + " pwd: " + pwd);
            // (2)、获取指定名称参数的所有值
        String[] hobbys = req.getParameterValues("hobby");
        System.out.println("hobbys: " + Arrays.toString(hobbys));
        if(hobbys != null && hobbys.length > 0) {
            for (String hobby : hobbys) {
                System.out.println("爱好: " + hobby);
            }
        }
    }
}
